/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, Collection, template, Model, dialog, datepicker) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'subsidy',
                model: {
                    url: _global.url.api + 'subsidy',
                    event: {
                        '.item-edit:click': 'edit',
                        '.item-remove:click': 'remove'
                    },
                    edit: function (e) {
                        e.preventDefault();
                        var item = this.data;
                        _this.dialog = dialog({
                            title: '修改补贴(' + this.data.gname + ')',
                            content: template('edit-all-tpl', this.data),
                            width: 700,
                            ok: function () {
                                _this.data.id = item.id;
                                _this.data.goods_id = $("input[name='goods_id']").val();
                                _this.data.admin_subsidy = $("input[name='admin_subsidy']").val();
                                _this.data.site_subsidy = $("input[name='site_subsidy']").val();
                                _this.data.start_time = $("input[name='start_time']").val() ? $("input[name='start_time']").val() : item.started_at;
                                _this.data.end_time = $("input[name='end_time']").val() ? $("input[name='end_time']").val() : item.ended_at;
                                _this.data.status = $("input[name='status']:checked").val();

                                if (new Model({
                                        url: _global.url.api + 'subsidy',
                                        data: _this.data
                                    }).put()) {
                                    _this.collection.reload();
                                }
                            }, cancel: function () {
                            }
                        });
                        _this.dialog.show();
                        $("input[name$=subsidy]").on('change', function (e) {
                            e.preventDefault();
                            var ratio = $(this).val();
                            var amount = Number(ratio / 100 * item.price).toFixed(2);
                            $(this).next('span').html('%(' + amount + '元)');
                        });
                        $('.long_date').on('click', function () {
                            WdatePicker({
                                el: this.id,
                                dateFmt: 'yyyy-MM-dd HH:mm:ss'
                            });
                        });

                    },
                    remove: function (e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function () {
                                if (model.del()) {
                                    _this.collection.reload();
                                }
                                ;
                            }, cancel: function () {
                            }
                        }).show(e.target);
                    }

                },

                callback: function () {
                    loaded();
                }
            });

            this.collection.fetch();
        },

        event: function () {
            this.data = {};
            var _this = this;
            //保存
            $('#btn-add').on('click', function (e) {
                _this.data = {};
                e.preventDefault();
                _this.dialog = dialog({
                    title: '添加定时补贴',
                    content: template('add-tpl', {}),
                    width: 600,
                    ok: function () {
                        _this.data.goods_id = $("input[name='goods_id']").val();
                        _this.data.admin_subsidy = $("input[name='admin_subsidy']").val();
                        _this.data.site_subsidy = $("input[name='site_subsidy']").val();
                        _this.data.start_time = $("input[name='start_time']").val();
                        _this.data.end_time = $("input[name='end_time']").val();
                        _this.data.status = $("input[name='status']:checked").val();


                        if (_this.data.start_time == '' || _this.data.end_time == '') {
                            Alert({msg: '开始时间或者结束时间不能为空', type: 'error'});
                            return false;
                        }
                        if (new Model({
                                url: _global.url.api + 'subsidy',
                                data: _this.data
                            }).post()) {
                            _this.collection.reload();
                        }
                    }, cancel: function () {
                    },
                });
                _this.dialog.show();

                $('#search').on('click', function (e) {
                    e.preventDefault();
                    var goods_id = $('#goods_id').val();
                    if (goods_id == '') {
                        Alert({msg: '请填写商品id进行搜索', type: 'error'});
                        return false;
                    }
                    $.ajax({
                        url: _global.url.api + 'promotion_search_goods',
                        data: {goods_id: goods_id},
                        type: 'get',
                        async: false,
                        success: function (res) {
                            if (res.code == 0) {
                                _this.item = res.data;
                                $('#search_form').hide();
                                $('#add_form').html(template('edit-tpl', res.data));
                                $('.long_date').on('click', function () {
                                    WdatePicker({
                                        el: this.id,
                                        dateFmt: 'yyyy-MM-dd HH:mm:ss'
                                    });
                                });
                            }
                        }
                    });

                    $("input[name$=subsidy]").on('change', function (e) {
                        e.preventDefault();
                        var ratio = $(this).val();
                        var amount = Number(ratio / 100 * _this.item.price).toFixed(2);
                        $(this).next('span').html('%(' + amount + '元)');
                    });
                });

                $('input,select,textarea').on('change', function (e) {
                    _this.change(e);
                });
            });

            //上传图片
            $('body').on('click', '#btn-upload', function (e) {
                _this.upload_dialog = dialog({
                    content: template('upload-tpl', {}),
                    width: 550,
                    ok: function () {
                        var img = $("#upload-frame")[0].contentWindow.image.get();
                        _this.imgPreview(img);
                    }, cancel: function () {
                    }
                });
                _this.upload_dialog.show();
            });
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.reload();
            });
        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        },

        imgPreview: function (img) {
            if (typeof img.id !== 'undefined') {
                this.data.img = img.id;
                $('#img-view').html(template('img-tpl', img));
            }
        }
    };

    new index();

});